apiready = function() {
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service","../../dialog"],function(V,$api,$,app,service,dialog){
	app.ready();
	var vm = {
		el: '.container',
	  	data: {
				inds: '',	//0 出售中商品标志；1 已下架商品标志
				on_sale: '',
	  		//--商品列表
	    	batchList: [],
	    	//--分类列表
	    	classifyList: [{
	    		name: '商品分类',
	    		id: '46589'
	    	},{
	    		name: '商品分类',
	    		id: '7963225'
	    	}],
	    	//--选择的商品id
	    	goods_id: '',
	    	//--选择的分类id
	    	classify_id: '',
				page: 1
	  	},
	  	mounted:function() {
				//--解决ios输入法挡住输入框的问题
				$(document).on("focus","#aui-input",function(){
					console.log($('body').scrollTop());
					var top = $('body').scrollTop();
					setTimeout(function(){
						console.log(document.body.offsetHeight);
						window.scrollTo(0,top+100);
					},50);
				});
	  	},
	  	methods:{
				// alert(JSON.stringify(goods_id));
				//--获取商品列表
				productListGet: function(on_sale, page, category_id, callback){	//获取商品列表
					var shopId = api.pageParam.shopId; // 商店id
					var unionid = $api.getStorage("shop_unionid"); // 用户id
					var on_sale = on_sale;
					var page = page;
					var category_id = category_id;
					var that = this;

					app.ajax(service['productListGet'],
					{
						data: {
							values: {
								shop_code: shopId,
								unionid: unionid,
								on_sale: on_sale,
								page: page,
								category_id: category_id
							}
						}
					},
					function (res) {
						if(res.code){
							alert("错误代码code："+res.code+"\n错误信息："+res.message)
						}else{
							callback(res)
							console.log("获取商品列表成功!");
						}
					});
				},
				//--获取分类列表
				getCat: function(){	//获取分类列表
					var shopId = api.pageParam.shopId; // 商店id
					var unionid = $api.getStorage("shop_unionid"); // 用户id
					var that = this;

					app.ajax(service['getCategory'],
					{
						data: {
							values: {
								unionid: unionid,
								shop_code: shopId
							}
						}
					},
					function (res) {
						// var shopCode = res.data.shop_code; // 商店id
						if(res.code){
							alert("错误号码code："+res.code+"\n错误信息："+res.message)
						}else{
							that.classifyList = res.data;
							console.log(res);
						}
					});

				},
	  		//----上下架
	  		saleStatus: function(){
					var shopId = api.pageParam.shopId; // 商店id
					var unionid = $api.getStorage("shop_unionid"); // 用户id
					var that = this;

					this.goods_id = '';

					//--获取选的商品
	  			var check = document.querySelectorAll(".batch_mge_list input");
	  			for(var i= 0; i < check.length; i++){
	  				if(check[i].checked){
	  					//--获取商品id放入数组
							if(this.goods_id==''){
								this.goods_id = check[i].dataset.goods_id;
							}else{
								this.goods_id += ','+check[i].dataset.goods_id;
							}
	  				}
	  			}
					if(this.goods_id != ''){
						if(this.inds == 1){
							var title = "批量上架商品提示";
							var msg = "请确认是否上架这些商品";
							var finish_msg = '商品已上架';
							var sale = 1;
						}else{
							var title = "批量下架商品提示";
							var msg = "请确认是否下架这些商品";
							var finish_msg = '商品已下架';
							var sale = 0;
						}
						dialog.alert({
							title: title,
							msg: msg,
							buttons: ['取消', '确定']
						}, function(ret) {
							if(ret.buttonIndex == 2){ //确认
								app.ajax(service['productOnSale'],
								{
									data: {
										values: {
											shop_code: shopId,
											unionid: unionid,
											goods_id: that.goods_id,
											on_sale: sale
										}
									}
								},
								function (res) {
									//返回category_id
									if(res.code){
										alert("上架接口错误代码code："+res.code+"\n错误信息："+res.message)
									}else{
										api.toast({
												msg: finish_msg,
												duration: 2000,
												location: 'middle'
										});
										//--取消选择
										$('.batch_mge_list input:checkbox').prop("checked", false)

										that.productListGet(that.on_sale, 1, '', function (res){
											that.batchList = res.data;
										});

										//--发送事件（使商品管理页面刷新）
										api.sendEvent({
										    name: 'backToGoodsManage',
										});
									}
								});
							}
						});
					}else{
						api.toast({
								msg: '请先选择商品...',
								duration: 2000,
								location: 'middle'
						});
					}
	  		},
	  		//----分类至
	  		classifyTo: function(){
					this.goods_id = '';
	  			//--获取选的商品
	  			var check = document.querySelectorAll(".batch_mge_list input");
	  			for(var i= 0; i < check.length; i++){
	  				if(check[i].checked){
							//--获取商品id放入字符串
							if(this.goods_id==''){
								this.goods_id = check[i].dataset.goods_id;
							}else{
								this.goods_id += ','+check[i].dataset.goods_id;
							}
	  				}
	  			}
	  			if(this.goods_id == ''){
						api.toast({
								msg: '请先选择商品...',
								duration: 2000,
								location: 'middle'
						});
						return
	  			}
	  			console.log(this.goods_id);
					$('body, html').css("z-index", "-1");
	  			document.querySelectorAll('.batch_classify')[0].style.display = "block";
					$('.batch_classify_list').css("overflow", "auto")
	  		},
				close_batch_classify: function(){
					$('.batch_classify').css("display", "none");
					this.classify_id = '';
				},
				getCategory: function(cat_id){
					console.log(this.classify_id);

					if(this.classify_id == cat_id){
						this.classify_id = '';
						$('.batch_classify').find('.aui-radio').prop('checked', false);
					}else{
						this.classify_id = cat_id;
					}
				},
	  		//--确定
	  		classifySure: function(){
					var shopId = api.pageParam.shopId; // 商店id
					var unionid = $api.getStorage("shop_unionid"); // 用户id
					var that = this;

	  			var check = document.querySelectorAll(".batch_classify_list input:checked");
	  			if(check.length <= 0){
						api.toast({
								msg: '请选择分类...',
								duration: 2000,
								location: 'middle'
						});
	  				return;
	  			}
	  			// this.classify_id = check[0].dataset.id;

					console.log(this.classify_id);

					app.ajax(service['productChangeCategory'],
					{
						data: {
							values: {
								shop_code: shopId,
								unionid: unionid,
								goods_id: this.goods_id,
								category_id: this.classify_id
							}
						}
					},
					function (res) {
						//返回category_id
						if(res.code){
							alert("错误号码code："+res.code+"\n错误信息："+res.message)
						}else{
							alert("商品分类成功");
							document.querySelectorAll('.batch_classify')[0].style.display = "none";
						}
					});

	  		},
	  		//--取消
	  		classifyCancel: function(){
	  			document.querySelectorAll('.batch_classify')[0].style.display = "none";
					this.classify_id = '';
					$('.batch_classify').find('.aui-radio').prop('checked', false);
	  		},
				//--新建分类
	  		newClassify: function(){
					var that = this;
					var shopId = api.pageParam.shopId; // 商店id
					var unionid = $api.getStorage("shop_unionid"); // 用户id

					dialog.prompt({
              title: "新建分类",
              text: "输入分类名称",
              buttons: ['取消', '确定']
          }, function(ret) {
            // alert(JSON.stringify(ret));
            if(ret.buttonIndex == 2){ //  确认
							var name = $api.trim(ret.text);

							if (name == '' || name == null) {
								api.toast({
										msg: "请输入正确的分类名称...",
										duration: 2000,
										location: 'middle'
								});
								name.value = '';
								return false;
							}else if(name.length > 10){
								api.toast({
										msg: "名称字数超过上限...",
										duration: 2000,
										location: 'middle'
								});
								return false;
							}
							//--判断是否存在特殊字符
							if(app.hasSpecialChar(name, '请正确输入分类名称...', '')){
								return
							}

							app.ajax(service['addCategory'],
							{
								data: {
									values: {
										shop_code: shopId,
										unionid: unionid,
										name: name
									}
								}
							},
							function (res) {
								//返回category_id
								if(res.code){
									alert("错误号码code："+res.code+"\n错误信息："+res.message)
								}else{
									that.category_id = res.data;
									console.log(this.category_id);
									alert("新建分类成功！");

									that.getCat();
								}
							});
            }
          })
	  		},
				// 触底
				scrolltobottom:function () {
					var that = this;
					this.page = parseInt(this.page + 1);

					this.productListGet(this.on_sale, this.page, '', function (res){
						if(res.data!=''){
							that.batchList = that.batchList.concat(res.data);
						}else{
							if(that.page != 1){
								that.page = parseInt(that.page - 1);
								api.toast({
						        msg: '没有更多数据...',
						        duration: 2000,
						        location: 'middle'
						    });
							}
						}
					});
				}
	  	},
	  	created: function(){
				var that = this;

				this.inds = api.pageParam.inds;	//0 出售中商品；1 已下架商品；
				if(this.inds == 1){
					this.on_sale = 2;
				}else{
					this.on_sale = 1;
				}
				//获取出售中商品列表
				this.productListGet(this.on_sale, 1, '', function (res){
					that.batchList = that.batchList.concat(res.data);
					// console.log(JSON.stringify(that.batchList));
				});

				this.getCat();

				api.addEventListener({
						name: 'scrolltobottom',
						extra:{
								threshold:0            //设置距离底部多少距离时触发，默认值为0，数字类型
						}
				}, function(ret, err){
						that.scrolltobottom()
				});
	  	}
	};

	var even = function() {}
	var newVue = new Vue(vm);
	even();
});
}
